package io.bdmc.modules.sys.controller;

import java.time.LocalDateTime;

import com.alibaba.fastjson2.JSONObject;
import io.bdmc.common.utils.*;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import io.bdmc.common.annotation.SysLogger1;
import io.bdmc.common.utils.Constant.ModifyName;
import io.bdmc.modules.sys.entity.SysAbout;
import io.bdmc.modules.sys.service.ISysAboutService;
import io.bdmc.core.snmp.SnmpDataCache;
import io.bdmc.core.snmp.SnmpSMSData;
import io.bdmc.core.snmp.cache.ConcurrentHashMapCacheUtils;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author 黄露
 * @since 2020-09-11
 */

// "系统关于内容", description = "/sys/about")
@RestController
@RequestMapping("/sys/about")
public class SysAboutController extends AbstractController {

	@Autowired
	ISysAboutService _sysaboutSvs;

	@Autowired
	SMSUtils smsUtils;

	@GetMapping("/info")
	// "查询系统关于内容", notes = "查询系统关于内容")
	public R list() {
		SysAbout about = _sysaboutSvs.list().get(0);
		Object obj = ConcurrentHashMapCacheUtils.getCache(Constant.Is162Startup);
		if (obj != null) {
			boolean is162Startup = (Boolean) obj;
			about.setIs162Startup(is162Startup);
			return R.ok().putData(about);
		}
		return R.ok().putData(about);
	}

	@SysLogger1(value = "修改关于内容", entityType = SysAbout.class, operationType = ModifyName.UPDATE)
	@PostMapping("/update")
	@RequiresPermissions("sys:about:update")
	// "修改关于内容", notes = "修改关于内容")
	public R update(@RequestBody SysAbout sysabout) {
		Boolean b = sysabout.updateById();
		return R.trueOrFalse(b);
	}

	@PostMapping("/sms/test")
	public R smsTest(@RequestBody SysAbout sysabout) {
		boolean result = smsUtils.testSMSGateway(sysabout.getSmsIp(), sysabout.getSmsPort());
		return R.trueOrFalse(result);
	}

	@PostMapping("/email/test")
	public R emailTest(@RequestBody SysAbout sysabout) {
		String[] arr = sysabout.getMailReceiver().split(",");
		for (String s : arr) {
			EmailUtils.sendHtmlMail(sysabout.getMailHost(), sysabout.getMailUsername(), sysabout.getMailPassword(),
					"测试邮件", "this is a test email", s);
		}

		return R.ok();
	}

	@PostMapping("/sms/send")
	public R smsTestSend(@RequestBody JSONObject jsonObject) {
		int count = 0;
		String mobileArr = jsonObject.getString("smsReceiver");
		String ip = jsonObject.getString("smsIp");
		String content = jsonObject.getString("content");
		Integer port = jsonObject.getInteger("smsPort");
		// content = "设备名称-设备SN-光缆名称-纤芯序号-恢复时间-FMS@000007-0003D3000007-董1-2芯-";
		content = content + DateUtils.formatLocalDateTimeDefault(LocalDateTime.now());
		for (String mobile : mobileArr.split(",")) {
			SnmpSMSData smsData = new SnmpSMSData(ip, port, mobile, content);
			boolean result = SnmpDataCache.offSmsQueue(smsData);
			// SnmpDataCache.offSmsQueue(smsData);
			// boolean result = smsUtils.sendSMSLong(ip, port, mobile, content);
			if (result)
				count++;
		}

		return R.ok().putData("发送成功数量：" + count);
	}

	@PostMapping("/data/clear")
	public R smsTestSend() {
		// 清除系统数据
		boolean result = _sysaboutSvs.deleteAllData();
		return R.trueOrFalse(result);
	}

}
